#include <iostream>
#include <SDL.h>
#include "Graphics.h"
#include "CGame.h"
#include "CFont.h"
#include "LoseState.h"

LoseState LoseState::m_LoseState;

using namespace std;

void LoseState::init()
{
    loseSprite = new CImage();
    loseSprite->loadImage("data/img/youlose.png");
    loseSprite->setPosition(150,150);
    loseFont = new CFont();
//    pauseFont->loadFont("data/fonts/lucida12.png", 112, 208);
    loseFont->loadFont("data/fonts/arialblack28.png", 448, 640);

    firstLoad = true;

    cout << "LoseState Init Successful" << endl;
}

void LoseState::cleanup()
{
    delete loseSprite;
	cout << "WinState Clean Successful" << endl;
}

void LoseState::resume() {}

void LoseState::pause() {}

void LoseState::handleEvents(CGame* game)
{
	SDL_Event event;

	if (SDL_PollEvent(&event)) {
		switch (event.type) {
			case SDL_QUIT:
				game->quit();
				break;

            case SDL_KEYDOWN:
				switch(event.key.keysym.sym){
                    case SDLK_p:
                        game->popState();
                        break;
                    case SDLK_ESCAPE:
                        game->quit();
                        break;
                    default:
                        break;
				}
		}
	}
}

void LoseState::update(CGame* game)
{
    if(firstLoad) {
        audio = game->getAudioEngine();
        loseTheme = audio->addSoundSourceFromFile("data/audio/game-over.wav");
        firstLoad = false;

        // play theme
        loseTheme->setDefaultVolume(0.4);
        audio->play2D(loseTheme);
    }
}

void LoseState::draw(CGame* game)
{
    loseSprite->setPosition(150 + game->getXpan(), 150 + game->getYpan());

    glClearColor(0,0,1,1); // blue
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    loseSprite->setScale(1);
    loseSprite->draw();
    glLoadIdentity();
    loseFont->draw(50 + game->getXpan(), 300 + game->getYpan(),"Press ESC to exit the game");
    SDL_GL_SwapBuffers();
}
